package com.ctl.utils.redis;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import redis.clients.jedis.*;

public class RedisShardedJedis {
	// 分布式直连同步调用
	public static void main(String[] args) {
		// 配置信息
		String host = "localhost"; // RedisConfig.host
		int port = 6379;           // RedisConfig.port
		String auth = "your_password"; // RedisConfig.auth

		// 创建Jedis池配置
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		// 设置最大连接数等其他配置...

		// 创建Jedis池
		try (JedisPool jedisPool = new JedisPool(poolConfig, host, port, Protocol.DEFAULT_TIMEOUT, auth)) {
			// 从池中获取Jedis实例
			try (Jedis jedis = jedisPool.getResource()) {
				// 如果需要验证密码
				if (!jedis.auth(auth).equals("OK")) {
					throw new RuntimeException("Failed to authenticate with Redis server");
				}

				// 使用管道
				long start = System.currentTimeMillis();
				try (Pipeline jedisPipeline = jedis.pipelined()) {
					for (int i = 0; i < 100; i++) {
						jedisPipeline.set("sppn" + i, "n" + i);
					}
					// 同步并返回所有结果
					List<Object> results = jedisPipeline.syncAndReturnAll();
					long end = System.currentTimeMillis();
					System.out.println("Pipelined SET: " + ((end - start) / 1000.0) + " seconds results.size()=" + results.size());
				}
			}
		}
	}
}
